<?php
/**
 * CidRepo.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 16:44:11
 * @modified    2023-02-14 16:44:11
 *
 */

namespace App\Repositories\User;

use App\Library\Logger;
use App\Models\User\Cid;

class CidRepo
{
    public static function addCid($userId, $platform, $cid)
    {
        $logger = new Logger('cid_repo');
        $logger->info("add cid: user_id: {$userId}, platform: {$platform}, cid: {$cid}");
        if (!$platform || !$cid) {
            return;
        }
        $exist = Cid::byUserId($userId)->byDeviceId($cid)->byPlatform($platform)->count();
        if ($exist) {
            return;
        }
        $cidModel = new Cid();

        Cid::query()->where('device_id', $cid)->delete();

        $cidModel->fill([
            'user_id' => $userId,
            'platform' => $platform,
            'device_id' => $cid
        ]);
        $cidModel->saveOrFail();
    }
}